'''
Author: liankafohali 1910083889@qq.com
Date: 2023-06-26 20:03:59
LastEditors: liankafohali 1910083889@qq.com
LastEditTime: 2023-07-29 17:10:27
FilePath: \led\test.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
from flask import Flask, jsonify, request,render_template
from flask_socketio import SocketIO, emit
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret_key'
socketio = SocketIO(app)
LedState = False

# 记录点击次数
click_count = 0

def get_light_status_from_server():
    return LedState

@app.route("/")
def index():
    return render_template("test.html")

@app.route('/sync-color-value', methods=['POST'])
def api_color():
    data = request.get_json()
    print(data)
    selected_color = data['color']
    # do something with selected_color
    return jsonify({'status': 'success'})

@app.route('/sync-timer-value', methods=['POST'])
def api_time():
    data = request.get_json()
    print(data)
    selected_Time = data['selectedTime']
    # do something with selected_color
    return jsonify({'status': 'success'})

@app.route('/sync-light-status', methods=['POST'])
def api_led():
    global LedState
    data = request.get_json()
    print(data)
    is_On = data['isOn']
    if is_On == True:
        LedState = True
    else:
        LedState = False
    # do something with selected_color
    return jsonify({'status': 'success'})

@app.route('/get-light-status', methods=['GET'])
def get_light_status():
    # 获取灯的状态，这里假设你已有相应的逻辑来获取灯的状态
    is_on = get_light_status_from_server()  # 请替换为你实际的获取灯状态的逻辑
    print(is_on)
    # 返回灯的状态
    return jsonify({'isOn': is_on})

@app.route('/003.jpg', methods=['GET'])
def get_jpg_status():
    with open('003.jpg', 'rb') as f:
        data = f.read()
    return data

@socketio.on('button_click')
def on_button_click():
    global click_count
    click_count += 1
    # 发送更新后的点击次数
    print('click_count'+str(click_count))
    emit('click_count', click_count)
    
if __name__ == '__main__':
    # from waitress import serve
    # serve(app, host="0.0.0.0", port=888)
    socketio.run(app, port=888)
        
